﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;

namespace MySqlConnection
{
    public class MySqlContext
    {
        /// <summary>
        /// List of data contexts
        /// </summary>
        private static Dictionary<string, DBContainer> _listContexts;

        /// <summary>
        /// Property to access list of data contexts
        /// </summary>
        public static Dictionary<string, DBContainer> ListContexts
        {
            get { return MySqlContext._listContexts; }
            set { MySqlContext._listContexts = value; }
        }

                /// <summary>
        /// Static constructor
        /// </summary>
        static MySqlContext()
        {
            MySqlContext._listContexts = new Dictionary<string, DBContainer>();
        }


        /// <summary>
        /// To access to data context
        /// </summary>
        public static DBContainer DataContext
        {
            get
            {
                DBContainer oDataContext = null;
                if (HttpContext.Current != null && HttpContext.Current.Session != null)
                {
                    if (ListContexts.ContainsKey(HttpContext.Current.Session.SessionID))
                    {
                        oDataContext = ListContexts[HttpContext.Current.Session.SessionID];
                    }
                    else
                    {
                        oDataContext = MySqlContext.CreateMySqlContext();
                        ListContexts.Add(HttpContext.Current.Session.SessionID, oDataContext);
                    }
                }
                else
                {
                    if (ListContexts.ContainsKey("AppliWin"))
                    {
                        oDataContext = ListContexts["AppliWin"];
                    }
                    else
                    {
                        oDataContext = MySqlContext.CreateMySqlContext();
                        ListContexts.Add("AppliWin", oDataContext);
                    }
                }
                return oDataContext;
            }
        }

        public static DBContainer CreateMySqlContext()
        {
            DBContainer ctxt = DBContainer.GetInstance();
            return ctxt;
        }
    }
}
